<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Admin\TvipCostOrder;
use Illuminate\Support\Facades\Session;

class UserController extends CommonController {

	/**
	 * 后台登录页面
	 * @param Request $request
	 * @return mixed
	 */
	public function login(Request $request){
		if($request->isMethod('post')){

			$username = $request->input('username');
			$password = $request->input('password');
			// if(!in_array($username,$this->mobile)){
			// 	$this->ajax_error('您没有授权登录操作');
			// }
			$userInfo = DB::table('ruzhu_merchant_basic')->where(array('CORPMAN_MOBILE' => $username))->first();
			if(!$userInfo){
				$this->ajax_error('用户名不存在');
			}

			//$md5_password = $this->merchant_pwd($username);
			$md5_password = $this->merchant_pwd($password);
			if($md5_password != $userInfo->pwd){
				$this->ajax_error('密码不正确');
			}else{
				$_SESSION['admin_uid'] = $userInfo->id;
                $_SESSION['node_id'] = $userInfo->node_id;//关联的用户ID
				$this->ajax_success('登录成功');
			}
		}
		// echo $this->merchant_pwd(13618624473);
		return view('admin.user.login');
	}

	/**
	 * 后台账号退出页面
	 */
	public function logout(){
		header('Content-Type: text/html; charset=utf-8');
		unset($_SESSION['admin_uid']);
		session_destroy();
		header( "Location:".asset('user/login'));
	}
	/**
	 * @param $mobile 商家手机号
	 * 商家后台登录密码加密规则：strtolower(md5(密码.'shop__'))
	 */
	protected function merchant_pwd($mobile){
		return strtolower(md5($mobile.'shop__'));
	}

	/**
	 * 修改密码
	 */
	public function modify_pwd(Request $request){
	$uid = $_SESSION['admin_uid'];
	if($request->isMethod('post')) {
	     $old_pwd = $request->input('old_pwd');
	     $new_pwd = $request->input('new_pwd');
	     $rnew_pwd = $request->input('rnew_pwd');
	     $userInfo = DB::table('ruzhu_merchant_basic')->where(array('id' => $uid))->first();
	     $old_pwd = $this->merchant_pwd($old_pwd);
	     if($old_pwd != $userInfo->pwd){
				$this->ajax_error('旧密码输入不正确');
			}elseif($new_pwd != $rnew_pwd){
				$this->ajax_error('两次密码输入不一致');
			}else{
				$updatepwd['pwd'] = $this->merchant_pwd($new_pwd);
				$res = DB::table('ruzhu_merchant_basic')->where(array('id' => $uid))->update($updatepwd);
				$this->ajax_success('修改成功');
			}
			
			
		}else{
			return view('admin.user.modify_pwd');
		}

	}
}